package com.example.jpetstoremanager.controller;

import com.example.jpetstoremanager.entity.Category;
import com.example.jpetstoremanager.entity.Item;
import com.example.jpetstoremanager.entity.Product;
import com.example.jpetstoremanager.persistence.CategoryMapper;
import com.example.jpetstoremanager.persistence.ItemMapper;
import com.example.jpetstoremanager.persistence.ProductMapper;
import com.example.jpetstoremanager.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import java.util.List;

@Controller
public class PetManageController {
    @Autowired
    public CategoryMapper categoryMapper;

    @Autowired
    public ProductMapper productMapper;

    @Autowired
    public ItemMapper itemMapper;

    @Autowired
    public CatalogService catalogService;

    @GetMapping("/petCategoryManage")
    public String petCategoryManage(Model model) {
        List<Category> categories = catalogService.findAllCategorys();
        model.addAttribute("categories", categories);
        return "petCategoryManage";
    }

    @GetMapping("/petProductManage")
    public String petProductManage(Model model) {
        List<Category> categories = catalogService.findAllCategorys();
        model.addAttribute("categories", categories);

        List<Product> products = catalogService.findAllProducts();
        model.addAttribute("products", products);
        return "petProductManage";
    }

    @GetMapping("/petItemManage")
    public String petItemManage(Model model) {
        List<Category> categories = catalogService.findAllCategorys();
        model.addAttribute("categories", categories);

        List<Product> products = catalogService.findAllProducts();
        model.addAttribute("products", products);

        List<Item> items = catalogService.allItems();
        model.addAttribute("items", items);
        return "petItemManage";
    }

    @GetMapping("/addCategory")
    public String addCategory(@ModelAttribute Category category) {
        catalogService.insertCategory(category);
        return "redirect:/petCategoryManage";
    }

    //增加品种
    @GetMapping("/addProduct")
    public String addItem(@ModelAttribute Product product) {
        catalogService.insertProduct(product);
        return "redirect:/petProductManage";
    }

    //增加宠物
    @GetMapping("/addItem")
    public String addItem(@ModelAttribute Item item) {
        catalogService.insertItem(item);
        return "redirect:/petItemManage";
    }

    @GetMapping("/editCategory")
    public String editCategory(@ModelAttribute Category category) {
        catalogService.updateCategoryById(category);
        return "redirect:/petCategoryManage";
    }

    //编辑品种
    @GetMapping("/editProduct")
    public String editProduct(@ModelAttribute Product product) {
        catalogService.updateProductById(product);
        return "redirect:/petProductManage";
    }

    //编辑宠物
    @GetMapping("/editItem")
    public String editItem(@ModelAttribute Item item) {
        catalogService.updateItemById(item);
        return "redirect:/petItemManage";
    }

    @CacheEvict(value = "", allEntries = true)
    @RequestMapping(value = "/deleteCategory", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteCategory(@RequestParam("categoryId") String categoryId, Model model) {
        catalogService.deleteCategoryById(categoryId);
        List<Category> categories = catalogService.findAllCategorys();
        model.addAttribute("categories", categories);
        return "redirect:/petCategoryManage";
    }

    //删除品种
    @CacheEvict(value = "", allEntries = true)
    @RequestMapping(value = "/deleteProduct", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteProduct(@RequestParam("productId") String productId, Model model) {
        catalogService.deleteProductById(productId);
        List<Product> products = catalogService.findAllProducts();
        model.addAttribute("products", products);
        return "redirect:/petProductManage";
    }

    //删除宠物
    @CacheEvict(value = "", allEntries = true)
    @RequestMapping(value = "/deleteItem", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteItem(@RequestParam("itemId") String itemId, Model model) {
        catalogService.deleteItemById(itemId);
        System.out.println(itemId);
        List<Item> items = catalogService.allItems();
        model.addAttribute("items", items);
        return "redirect:/petItemManage";
    }

}
